<?php
/**
 * 课程目录
 * Class CourseAction
 */
class CourseAction extends CommonAction
{
    /**
     * 课程下面的信息
     */
    public function item()
    {
        $itemid = intval(I('get.itemid'));
        $this->checkEmpty($itemid, '标示不正确！');
        $course = M('Course')->find($itemid);
        $this->checkEmpty($course, '课程信息不存在！');
        $StudyModel = D('Study');
        $map['cid']= $itemid;
        import('ORG.Util.Page');
        $count = $StudyModel->where($map)->count();
        $Page = new Page($count,15);
        $studys = $StudyModel->limit($Page->firstRow.','.$Page->listRows)->where($map)->select();
        $this->assign('page', $Page->show());
        $this->assign('studys', $studys);
        $this->assign('course', $course);
        $this->display();
    }

    public function add() {
        
        $lang = I('post.lang');
        $lev = I('post.lev');

        $itemid = parent::_add(0,0,1);

        $data = array();
        $data['lang'] = $lang;
        $data['lev'] = $lev;
        $data['itemid'] = $itemid;

        $TagModel = D("Tag");
        $flag = $TagModel->add($data);

        if (!empty($flag)) { //保存成功
                $this->success('添加成功！', U('/Admin/Course'));
        }

    }

    public function edit() {
        $isOk = parent::_edit(true);

        if($isOk){
            //修改tag
            //find
            //add OR update
            $itemid = I('get.itemid');

            $TagModel = D('Tag');

            $tag = $TagModel->where("itemid = $itemid")->select();

            $data = array();
            $data = $TagModel->create($data);

            if ($tag) {
                $rs = $TagModel->where("itemid = $itemid")->save($data);
                if ($rs !== false) {
                    $this->success('修改成功！', U('/Admin/Course'));
                    exit;
                }
            } else {
                $rs = $TagModel->add($data);
                if (!empty($rs)) {
                    $this->success('添加成功！', U('/Admin/Course'));
                    exit;
                }
            }


        } else {
            $this->error('修改失败！');
        }

        $this->assign('vo', $isOk);
        $this->assign('tag', $tag);
        $this->display();
    }
}